﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for CSMSWebService
/// </summary>
[WebService(Namespace = "http://cocshopfpt.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class CSMSWebService : System.Web.Services.WebService {
    CSMSDataContext db = null;
    public CSMSWebService () {
        db = new CSMSDataContext();
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }


    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    #region Accounts
    [WebMethod]
    public void AddNewAccount(Account acc)
    {
        db.Accounts.InsertOnSubmit(acc);
        db.SubmitChanges();
    }

    [WebMethod]
    public List<Account> GetListAccount()
    {
        List<Account> listAcc = db.Accounts.ToList();
        foreach (Account acc in listAcc)
        {
            int roleId = acc.roleId;
            acc.Employees.Clear();
            acc.Customers.Clear();
            acc.Role = null;
            acc.roleId = roleId;
        }
        return listAcc;
    }

    [WebMethod]
    public void DeleteAccount(int id)
    {
        var tmp = db.Accounts.Single(a => a.accId == id);
        db.Accounts.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateAccount(int id, Account acc)
    {
        var tmp = db.Accounts.Single(a => a.accId == id);
        tmp.username = acc.username;
        tmp.password = acc.password;
        tmp.roleId = acc.roleId;
        tmp.isActive = acc.isActive;
        db.SubmitChanges();
    } 
    #endregion

    #region Category
    [WebMethod]
    public List<Category> GetListCategory()
    {
        List<Category> listCate = db.Categories.ToList();
        foreach (Category item in listCate)
        {
            item.Products.Clear();
        }
        return listCate;
    }

    [WebMethod]
    public void AddNewCategory(Category cate)
    {
        db.Categories.InsertOnSubmit(cate);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateCategory(int id, Category cate)
    {
        var tmp = db.Categories.Single(c => c.categoryId == id);
        tmp.categoryName = cate.categoryName;
        tmp.description = cate.description;
        db.SubmitChanges();
    }

    [WebMethod]
    public void DeleteCategory(int id)
    {
        var tmp = db.Categories.Single(c => c.categoryId == id);
        db.Categories.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    } 
    #endregion

    #region Customer
    [WebMethod]
    public List<Customer> GetListCustomer()
    {
        List<Customer> listCus = db.Customers.ToList();
        foreach (Customer cus in listCus)
        {
            int accid = cus.accId;
            cus.Account = null;
            cus.Orders.Clear();
            cus.accId = accid;
        }
        return listCus;
    }
    
    [WebMethod]
    public void AddNewCustomer(Customer cus)
    {
        db.Customers.InsertOnSubmit(cus);
        db.SubmitChanges();
    }


    [WebMethod]
    public void DeleteCustomer(int id)
    {
        var tmp = db.Customers.Single(c => c.cusId == id);
        db.Customers.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateCustomer(int id, Customer cus)
    {
        var tmp = db.Customers.Single(t => t.cusId == id);
        tmp.cusName = cus.cusName;
        tmp.cusSex = cus.cusSex;
        tmp.cusPhone = cus.cusPhone;
        tmp.cusAddress = cus.cusAddress;
        tmp.cusEmail = cus.cusEmail;
        db.SubmitChanges();
    }
    #endregion

    #region Employee
    [WebMethod]
    public List<Employee> GetListEmployee()
    {
        List<Employee> listEmp = db.Employees.ToList();
        foreach (Employee emp in listEmp)
        {
            int accid = emp.accId;
            int roleId = emp.roleId;
            emp.Account = null;
            emp.Role = null;
            emp.Expenses.Clear();
            emp.Imports.Clear();
            emp.Orders.Clear();
            emp.accId = accid;
            emp.roleId = roleId;
        }
        return listEmp;
    }

    [WebMethod]
    public void AddNewEmployee(Employee emp)
    {
        db.Employees.InsertOnSubmit(emp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void DeleteEmployee(int id)
    {
        var tmp = db.Employees.Single(c => c.empId == id);
        db.Employees.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateEmployee(int id, Employee emp)
    {
        var tmp = db.Employees.Single(e => e.empId == id);
        tmp.empName = emp.empName;
        tmp.roleId = emp.roleId;
        tmp.empSex = emp.empSex;
        tmp.empBirthdate = emp.empBirthdate;
        tmp.empHireDate = emp.empHireDate;
        tmp.empAddress = emp.empAddress;
        tmp.empPhone = emp.empPhone;
        db.SubmitChanges();
    }
    #endregion

    #region Order
    [WebMethod]
    public List<Order> GetListOrder()
    {
        List<Order> listOrder = db.Orders.ToList();
        foreach (Order order in listOrder)
        {
            int empid = 0;
            if (order.empId != null)
                 empid = (int) order.empId;
            int cusid = order.cusId;
            order.Employee = null;
            order.Customer = null;
            order.OrderDetails.Clear();
            order.empId = empid;
            order.cusId = cusid;
        }
        return listOrder;
    }

    [WebMethod]
    public void AddNewOrder(Order order)
    {
        db.Orders.InsertOnSubmit(order);
        db.SubmitChanges();
    }


    [WebMethod]
    public void DeleteOrder(int id)
    {
        var tmp = db.Orders.Single(o => o.orderId == id);
        db.Orders.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateOrder(int id, Order order)
    {
        var tmp = db.Orders.Single(o => o.orderId == id);
        tmp.empId = order.empId;
        tmp.orderAddress = order.orderAddress;
        tmp.orderTime = order.orderTime;
        tmp.orderPhone = order.orderPhone;
        tmp.orderStatus = order.orderStatus;
        db.SubmitChanges();
    }
    #endregion

    #region Product
    [WebMethod]
    public List<Product> GetListProduct()
    {
        List<Product> listPro = db.Products.ToList();
        foreach (Product pro in listPro)
        {
            int categoryId = pro.categoryId;
            pro.Category = null;
            pro.ImportDetails.Clear();
            pro.OrderDetails.Clear();
            pro.categoryId = categoryId;
        }
        return listPro;
    }

    [WebMethod]
    public void AddNewProduct(Product pro)
    {
        db.Products.InsertOnSubmit(pro);
        db.SubmitChanges();
    }


    [WebMethod]
    public void DeleteProduct(int id)
    {
        var tmp = db.Products.Single(p => p.productId == id);
        db.Products.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateProduct(int id, Product pro)
    {
        var tmp = db.Products.Single(p => p.productId == id);
        tmp.productName = pro.productName;
        tmp.categoryId = pro.categoryId;
        tmp.unitPrice = pro.unitPrice;
        tmp.productQuantity = pro.productQuantity;
        tmp.isActive = pro.isActive;
        db.SubmitChanges();
    }
    #endregion

    #region Role
    [WebMethod]
    public List<Role> GetListRole()
    {
        List<Role> listRole = db.Roles.ToList();
        foreach (Role role in listRole)
        {
            role.Accounts.Clear();
            role.Employees.Clear();
        }
        return listRole;
    }

    [WebMethod]
    public void AddNewRole(Role role)
    {
        db.Roles.InsertOnSubmit(role);
        db.SubmitChanges();
    }


    [WebMethod]
    public void DeleteRole(int id)
    {
        var tmp = db.Roles.Single(r => r.roleId == id);
        db.Roles.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateRole(int id, Role role)
    {
        var tmp = db.Roles.Single(r => r.roleId == id);
        tmp.roleName = role.roleName;
        db.SubmitChanges();
    }
    #endregion

    #region Import
    [WebMethod]
    public List<Import> GetListImport()
    {
        List<Import> listImp = db.Imports.ToList();
        foreach (Import imp in listImp)
        {
            int empId = imp.empId;
            imp.ImportDetails.Clear();
            imp.Employee = null;
            imp.empId = empId;
        }
        return listImp;
    }

    [WebMethod]
    public void AddNewImport(Import imp)
    {
        db.Imports.InsertOnSubmit(imp);
        db.SubmitChanges();
    }


    [WebMethod]
    public void DeleteImport(int id)
    {
        var tmp = db.Imports.Single(i => i.importId == id);
        db.Imports.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateImport(int id, Import imp)
    {
        var tmp = db.Imports.Single(i => i.importId == id);
        tmp.empId = imp.empId;
        tmp.importTime = imp.importTime;
        db.SubmitChanges();
    }
    #endregion

    #region Expense
    [WebMethod]
    public List<Expense> GetListExpense()
    {
        List<Expense> listExp = db.Expenses.ToList();
        foreach (var exp in listExp)
        {
            int empId = exp.empId;
            exp.Employee = null;
            exp.empId = empId;
        }
        return listExp;
    }

    [WebMethod]
    public void AddNewExpense(Expense exp)
    {
        db.Expenses.InsertOnSubmit(exp);
        db.SubmitChanges();
    }


    [WebMethod]
    public void DeleteExpense(int id)
    {
        var tmp = db.Expenses.Single(e => e.expenseId == id);
        db.Expenses.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateExpense(int id, Expense exp)
    {
        var tmp = db.Expenses.Single(e => e.expenseId == id);
        tmp.expenseDescription = exp.expenseDescription;
        tmp.empId = exp.empId;
        tmp.expenseTime = exp.expenseTime;
        tmp.totalMoney = exp.totalMoney;
        db.SubmitChanges();
    }
    #endregion

    #region OrderDetail
    [WebMethod]
    public List<OrderDetail> GetListOrderDetail(int orderId)
    {
        List<OrderDetail> listOrderDetail = db.OrderDetails.Where(o => o.orderId == orderId).ToList();
        foreach (OrderDetail orderDetail in listOrderDetail)
        {
            int proId = orderDetail.productId;
            int orId = orderDetail.orderId;
            orderDetail.Product = null;
            orderDetail.Order = null;
            orderDetail.productId = proId;
            orderDetail.orderId = orId;
        }
        return listOrderDetail;
    }

    [WebMethod]
    public void AddNewOrderDetail(OrderDetail orderDetail)
    {
        db.OrderDetails.InsertOnSubmit(orderDetail);
        db.SubmitChanges();
    }


    [WebMethod]
    public void DeleteOrderDetail(int orderId, int productId)
    {
        var tmp = db.OrderDetails.Single(o => o.orderId == orderId && o.productId == productId);
        db.OrderDetails.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateOrderDetail(int orderId, int productId, OrderDetail orderDetail)
    {
        var tmp = db.OrderDetails.Single(o => o.orderId == orderId && o.productId == productId);
        tmp.unitPrice = orderDetail.unitPrice;
        tmp.quantity = orderDetail.quantity;
        db.SubmitChanges();
    }
    #endregion

    #region ImportDetail
    [WebMethod]
    public List<ImportDetail> GetListImportDetail(int importId)
    {
        List<ImportDetail> listImportDetail = db.ImportDetails.Where(i => i.importId == importId).ToList();
        foreach (ImportDetail importDetail in listImportDetail)
        {
            int proId = importDetail.productId;
            int impId = importDetail.importId;
            importDetail.Product = null;
            importDetail.Import = null;
            importDetail.productId = proId;
            importDetail.importId = impId;
        }
        return listImportDetail;
    }

    [WebMethod]
    public void AddNewImportDetail(ImportDetail ImportDetail)
    {
        db.ImportDetails.InsertOnSubmit(ImportDetail);
        db.SubmitChanges();
    }


    [WebMethod]
    public void DeleteImportDetail(int importId, int productId)
    {
        var tmp = db.ImportDetails.Single(i => i.importId == importId && i.productId == productId);
        db.ImportDetails.DeleteOnSubmit(tmp);
        db.SubmitChanges();
    }

    [WebMethod]
    public void UpdateImportDetail(int importId, int productId, ImportDetail importDetail)
    {
        var tmp = db.ImportDetails.Single(i => i.importId == importId && i.productId == productId);
        tmp.importPrice = importDetail.importPrice;
        tmp.quantity = importDetail.quantity;
        db.SubmitChanges();
    }
    #endregion
}
